package chatclient.applets;

import chatclient.applets.canvas.ViewHeader;
import chatclient.applets.components.Border;
import chatclient.netio.AttrList;
import chatclient.netio.ClientAttr;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

public class ChatPanel extends JPanel
{
    protected ChatView parent;
    
    private ViewHeader header;    
    protected int backgroundInfoNr;
    private String bgimageName;
    private Image bgimage;   
    private Image bgbuffer;
      
    public ChatPanel(ChatView parent)
    {
        this.parent = parent;
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        if (!isVisible()) {
            return;
        }

        int i = size().width;
        int j = size().height;
        if (bgbuffer != null && (bgbuffer.getWidth(null) != i || bgbuffer.getHeight(null) != j)) {
            bgbuffer.flush();
            bgbuffer = null;
        }
        if (bgbuffer == null) {
            bgbuffer = createImage(i, j);
        }
        Graphics g1 = bgbuffer.getGraphics();
        g1.setColor(getBackground());
        g1.fillRect(0, 0, i, j);
        g1.setColor(getForeground());

        g.drawImage(bgbuffer, 0, 0, getBackground(), this);
    }
    
    
     protected void initChatView() {
                                                
        setLayout(new BorderLayout());
        String s;
        backgroundInfoNr = 2;

        Color color;
        setBackground((color = parent.getColor("bgcolor")) != null ? color : Color.white);
        setForeground((color = parent.getColor("fgcolor")) != null ? color : Color.black);

        setFont(new Font((s = parent.getParameter("font")) != null ? s : "SansSerif", 0, (s = parent.getParameter("fontSize")) != null ? Integer.parseInt(s) : 11));
        String s1;
        if ((s1 = parent.getParameter("header")) != null) {
            Border border = (s = parent.getParameter("headerBorder")) != null ? parent.createBorder(s) : null;
            this.header = new ViewHeader(parent, "header", s1, border);
            Color color1;
            this.header.setBackground((color1 = parent.getColor("bgcolor", "header")) != null ? color1 : getBackground());
            this.header.setForeground((color1 = parent.getColor("fgcolor", "header")) != null ? color1 : getForeground());
            this.header.setFont(getFont());
            add(this.header, "North");
        }
        
        validate();
    }        
 
     protected void cleanupChatView() {
         hide();
     }
     
    protected void updateBackground() {
        String s = null;
        
        Chat chat = parent.getChat();
        
        if (backgroundInfoNr != -1 && chat.getMyChannelInfo() != null) {
            ClientAttr clientattr = (ClientAttr) chat.getMyChannelInfo().get(ClientAttr.CHANNELINFO, backgroundInfoNr);
            if (clientattr != null) {
                try {
                    s = clientattr.getString();
                } catch (Exception exception1) {
                }
            }
        }
        if (s == null || s.length() == 0) {
            s = parent.getParameter("bgimage");
        }
        if (s == null) {
            bgimage = null;
        } else if (!s.equalsIgnoreCase(bgimageName)) {
            try {
                bgimage = parent.getBackgroundImage(s);
                bgimageName = s;
            } catch (Exception exception) {
                bgimage = null;
                bgimageName = null;
                exception.printStackTrace();
            }
            parent.updatedBgimage(bgimage);
        }
    }     
 
    protected void updateHeader() {
        if (header == null) {
            return;
        }
                
        Chat chat = parent.getChat();
                
        AttrList attrlist;
        try {
            attrlist = (AttrList) chat.getMyChannelInfo().clone();
        } catch (Exception exception) {
            attrlist = null;
        }
        if (attrlist == null) {
            attrlist = new AttrList();
        }
        try {
            attrlist.set(new ClientAttr(ClientAttr.NICKNAME, chat.getMyName()));
            if (chat.getMyChannel() != null) {
                attrlist.set(new ClientAttr(ClientAttr.CHANNELNAME, chat.getMyChannel()));
            }
        } catch (Exception exception1) {
        }
        header.setAttrs(attrlist);
    }    
    
}